package com.seckill.service;


import com.alibaba.fastjson.JSON;
import com.seckill.model.KillGoods;
import com.seckill.model.killorderinfo;
import com.seckill.model.orderinfo;
import com.seckill.service.KillGoodsService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class RabbitConsumerKill {

    @Autowired
    KillGoodsService killGoodsService;
    @Autowired
    com.seckill.service.orderinfoService orderinfoService;
    @Autowired
    com.seckill.service.killorderinfoService killorderinfoService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RabbitListener(queues = {"killQueue"})
    public void queueMap(Map map){
        ValueOperations<String, String> value = stringRedisTemplate.opsForValue();

        int userid = (int) map.get("userid");
        int killid = (int) map.get("killid");

        KillGoods killGoods =JSON.parseObject(value.get("seckill:kill:" + killid),KillGoods.class);

        int i = killGoodsService.KillGoodsDace(killid);

        if(i>0){
            orderinfo orderinfo = new orderinfo();
            orderinfo.setUserid(userid);
            orderinfo.setGoodsid(killGoods.getGoodsid());
            orderinfo.setGoodsname(killGoods.getGoodsname());
            orderinfo.setGoodscount(1);
            orderinfo.setOrderstatus(0);
            orderinfo.setCreatedate(new Date());
            orderinfo.setGoodsprice(killGoods.getGoodsprice());

            orderinfoService.save(orderinfo);

            killorderinfo killorderinfo = new killorderinfo();

            killorderinfo.setOrderid(orderinfo.getOrderid());
            killorderinfo.setGoodsid(killGoods.getGoodsid());
            killorderinfo.setUserid(userid);
            killorderinfoService.save(killorderinfo);
        }else {
            return;
        }

    }

}
